package cn.hxzy.springbootshiro;


import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
public class ExceptionHandleController {



    @ExceptionHandler(AuthenticationException.class)
    public String authenticationExceptionHandel(AuthenticationException e) {

        String msg;
        if (e instanceof IncorrectCredentialsException) {
            msg = "【登录密码错误】";
        } else if (e instanceof ExcessiveAttemptsException) {
            msg = "【登录失败次数过多,五分钟后再试】";
        } else if (e instanceof LockedAccountException) {
            msg = "【帐号已被锁定】";
        } else if (e instanceof DisabledAccountException) {
            msg = "【帐号已被禁用】.";
        } else if (e instanceof ExpiredCredentialsException) {
            msg = "【帐号已经过期】";
        } else if (e instanceof UnknownAccountException) {
            msg = "【帐号不存在】";
        } else if (e instanceof AccountException) {
            msg = e.getMessage();
        } else {
            msg = "【未知异常,登录失败】" + e.getMessage();
        }
        return msg + ":<a href='/'>跳转</a>";
    }

    @ExceptionHandler(AuthorizationException.class)
    public String authorizationExceptionHandel(AuthorizationException e) {
        return "权限不足:<a href='/'>跳转</a>";
    }


    @ExceptionHandler(UnauthenticatedException.class)
    public String unAuthenticationExceptionHandle(UnauthenticatedException e) {
        return "用户未登录或认证:<a href='/'>跳转</a>";
    }


    @ExceptionHandler(UnauthorizedException.class)
    public String UnauthorizedExceptionHandle(UnauthorizedException e) {
        return "未认证:<a href='/'>跳转</a>";
    }

    @ExceptionHandler(Throwable.class)
    public String throwableHandle(Throwable e) {
        return "全部异常:<a href='/'>跳转</a>";
    }


}
